package com.ruoyi.warning.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.warning.domain.Warning;
import com.ruoyi.warning.service.WarningService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 每月预警值Controller
 *
 * @author zjh
 * @date 2022-06-09
 */
@Controller
@RequestMapping("/warning")
public class WarningController extends BaseController
{
    private String prefix = "warning";

    @Autowired
    private WarningService warningService;

    @RequiresPermissions("business:warning:view")
    @GetMapping()
    public String warning()
    {
        return prefix + "/warning";
    }

    /**
     * 查询每月预警值列表
     */
    @RequiresPermissions("business:warning:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Warning warning)
    {
        startPage();
        List<Warning> list = warningService.selectWarningListWhichLack(warning);
        return getDataTable(list);
    }

    /**
     * 导出每月预警值列表
     */
    @RequiresPermissions("business:warning:export")
    @Log(title = "每月预警值", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Warning warning)
    {
        List<Warning> list = warningService.selectWarningList(warning);
        ExcelUtil<Warning> util = new ExcelUtil<Warning>(Warning.class);
        return util.exportExcel(list, "每月预警值数据");
    }

    /**
     * 新增保存每月预警值
     */
//    @RequiresPermissions("system:warning:add")
    @Log(title = "每月预警值", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Warning warning)
    {
        return toAjax(warningService.insertWarning(warning));
    }

    /**
     * 修改每月预警值
     */
    @GetMapping("/edit/{warningId}")
    public String edit(@PathVariable("warningId") Long warningId, ModelMap mmap)
    {
        Warning warning = warningService.selectWarningById(warningId);
        mmap.put("warning", warning);
        return prefix + "/edit";
    }

    /**
     * 修改保存每月预警值
     */
    @RequiresPermissions("business:warning:edit")
    @Log(title = "每月预警值", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Warning warning)
    {
        return toAjax(warningService.updateWarning(warning));
    }

    /**
     * 删除每月预警值
     */
    @RequiresPermissions("business:warning:remove")
    @Log(title = "每月预警值", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(warningService.deleteWarningByIds(ids));
    }
}

